<template>
  <el-dialog
    class="basic-dialog"
    :model-value="visible"
    :title="dialogTitle"
    :width="500"
    @closed="handleClosed"
    @open="handleDialogOpen"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-position="top"
      require-asterisk-position="right"
    >
      <!-- <el-form-item prop="domain" label="DNS服务器域名" required>
        <el-input
          v-model="formData.domain"
          placeholder="请输入DNS服务器域名，如：example.com"
        ></el-input>
      </el-form-item> -->
      <el-form-item label="DNS服务器地址" prop="addr" required>
        <el-input
          v-model="formData.addr"
          placeholder="请输入DNS服务器地址，如：ns1.example.com"
        ></el-input>
      </el-form-item>
      <!--
      <el-form-item label="IP地址" prop="ip" required>
        <el-input v-model="formData.ip" placeholder="请输入IP地址"></el-input>
      </el-form-item>
      -->
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          maxlength="200"
          resize="none"
          :autosize="{ minRows: 1, maxRows: 3 }"
          placeholder="请输入备注"
        ></el-input>
      </el-form-item>
      <el-form-item label="自动分配" prop="assign_status" label-position="left">
        <basic-switch v-model="formData.assign_status"></basic-switch>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button type="primary" :loading="loading" @click="handleSubmit">
        确定
      </el-button>
      <el-button :loading="loading" @click="emit('update:visible', false)"
        >取消</el-button
      >
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { computed } from 'vue'

import type { DnsServerModel } from '@/apis/model/dns'

import useForm from '../hooks/useForm'

interface IProps {
  visible: boolean
  loading?: boolean
  record?: DnsServerModel
}

interface IEmits {
  (event: 'update:visible', value: boolean): void
  (event: 'update:loading', value: boolean): void
  (event: 'reload'): void
}

const emit = defineEmits<IEmits>()
const props = defineProps<IProps>()

const dialogTitle = computed(() => {
  return props.record?.id ? '编辑DNS服务器' : '添加DNS服务器'
})

const { formRef, formData, formRules } = useForm()

/**
 * 保存方法
 */
const handleSubmit = async () => {
  await formRef.value?.validate()
  emit('update:loading', true)
  try {
    if (props.record?.id) {
      await putDnsServerApi({
        id: props.record?.id as string,
        ...formData
      })
    } else {
      await postDnsServerApi(formData)
    }
    emit('reload')
    emit('update:visible', false)
  } finally {
    emit('update:loading', false)
  }
}

/**
 * @description dialog 打开事件
 */
const handleDialogOpen = () => {
  if (props.record) {
    // formData.domain = props.record.domain
    formData.addr = props.record.addr
    // formData.ip = props.record.ip
    formData.remark = props.record.remark
    formData.assign_status = props.record.assign_status
  }
}
/**
 * @description dialog 关闭前事件
 */
const handleClosed = () => {
  emit('update:visible', false)
  formRef.value?.resetFields()
}
</script>

<style scoped></style>
